package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.*;
import com.travel.travelServer.entity.vo.StoreVo;
import com.travel.travelServer.mapper.*;
import com.travel.travelServer.service.StoreService;
import com.travel.travelServer.utils.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Store)表服务实现类
 *
 * @author makejava
 * @since 2024-03-03 14:05:09
 */
@Service("storeService")
public class StoreServiceImpl extends BaseService<StoreMapper, Store> implements StoreService {

    @Resource
    private UserMapper userDao;

    @Resource
    private ApplyMapper applyDao;

    @Resource
    private StoreCategoryMapper storeCategoryDao;

    @Resource
    private StoreFruitMapper storeFruitDao;

    @Override
    public Page<Store> page(Page<Store> page, Map<String, Object> conditions) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Store> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(conditions.get("name")) && conditions.get("name") != "", Store::getName, conditions.get("name"));
        wrapper.eq(Store::getUserId, userId);
        Page<Store> storePage = super.page(page,wrapper);
        storePage.getRecords().stream().forEach(store -> {
            if (Objects.nonNull(store.getUserId())){
                store.setUser(userDao.selectById(store.getUserId()));
            }
        });
        return storePage;
    }

    @Override
    public Store getById(Long id) {
        return super.getById(id);
    }

    @Override
    public Boolean add(Store pojo) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Apply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Apply::getApplyUserId, userId);
        Apply apply = applyDao.selectOne(wrapper);
        if (Objects.isNull(apply) || apply.getStatus() != 2){
//            throw new ServiceException("请先通过申请");
            return false;
        }
        pojo.setUserId(userId);
        return super.save(pojo);
    }

    @Override
    public Boolean update(Store pojo) {
        return super.updateById(pojo);
    }

    @Override
    public Boolean removeOne(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean batchRemove(List<Long> asList) {
        return super.removeByIds(asList);
    }

    @Override
    public Boolean batchAddCategory(StoreVo storeVo) {
        Long userId = getCurrentUser().getId();
        Arrays.asList(storeVo.getIds()).stream().forEach(aLong -> {
            StoreCategory storeCategory = new StoreCategory();
            storeCategory.setCategoryId(aLong);
            storeCategory.setStoreId(storeVo.getId());
            storeCategory.setUserId(userId);
            storeCategoryDao.insert(storeCategory);
        });
        return true;
    }

    @Override
    public Boolean batchAddFruit(StoreVo storeVo) {
        Long userId = getCurrentUser().getId();
        Arrays.asList(storeVo.getIds()).forEach(aLong -> {
            StoreFruit storeFruit = new StoreFruit();
            storeFruit.setFruitId(aLong);
            storeFruit.setStoreId(storeVo.getId());
            storeFruit.setUserId(userId);
            storeFruitDao.insert(storeFruit);
        });
        return true;
    }

    @Override
    public Boolean deleteMyCategoryById(Long id) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<StoreCategory> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(StoreCategory::getCategoryId, id);
        categoryLambdaQueryWrapper.eq(StoreCategory::getUserId, userId);
        storeCategoryDao.delete(categoryLambdaQueryWrapper);
        return true;
    }

    @Override
    public Boolean deleteMyFruitById(Long id) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<StoreFruit> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(StoreFruit::getFruitId, id);
        categoryLambdaQueryWrapper.eq(StoreFruit::getUserId, userId);
        storeFruitDao.delete(categoryLambdaQueryWrapper);
        return true;
    }
}

